package com.fy56.platform.goodsTracking.quartz;

import com.fy56.platform.goodsTracking.api.UploadApi;
import com.fy56.platform.goodsTracking.car.quartz.CarUpload;
import com.fy56.platform.goodsTracking.commons.DateUtil;
import com.fy56.platform.goodsTracking.commons.security.MD5;
import com.fy56.platform.goodsTracking.jdbc.datasource.DBManage;
import com.fy56.platform.goodsTracking.model.Jrpt;
import com.fy56.platform.goodsTracking.police.quartz.PoliceUpload;
import com.fy56.platform.goodsTracking.queue.CollectionLinkedQueue;
import com.fy56.platform.goodsTracking.queue.CollectionPoll;
import com.fy56.platform.goodsTracking.service.JrptService;
import com.fy56.platform.goodsTracking.web.UserToken;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by XTK on 2016-08-18.
 */
@Service
public class QuartzUpload {
    @Autowired
    private UploadApi uploadApi;
    @Autowired
    private JrptService jrptService;
    @Autowired
    private PoliceUpload policeUpload;
    @Autowired
    private CarUpload carUpload;

    static Logger log = Logger.getLogger(QuartzUpload.class);

    public void upload() {
         int iloop = 0;
       log.info("执行上传操作(" + DateUtil.Now() + ")");
        //查询所有需要上传的组织代码
        List<Jrpt> jrpts = jrptService.select();
        for (Jrpt jrpt : jrpts) {
            log.info("执行上传操作:Organizationcode:" + jrpt.getOrganizationcode() +
                    ";Losttime:" + jrpt.getLosttime()
                    + ";---开始---");
            try {
                String token = uploadApi.doLogin(jrpt);
                if (token.equals("")) {
                    continue;
                }

                UserToken userToken = new UserToken();
                userToken.setUserName(jrpt.getJrptuser());
                userToken.setUserPassWord(MD5.digest(jrpt.getJrptpass()).toUpperCase());
                String endTime = DateUtil.before(60);
                uploadApi.doUpload_Main(jrpt, userToken,endTime);
                try {
                    Thread.sleep(10000);//休息一会
                } catch (InterruptedException ire) {
                }
                uploadApi.doUpload_Sub(jrpt, userToken,endTime);
            } catch (Exception e) {
                log.error("执行上传操作:Organizationcode:" + jrpt.getOrganizationcode() +
                        ";Losttime:" + jrpt.getLosttime()
                        + ";---错误---" + e.getMessage());
            }
        }
        try {
            Thread.sleep(10000);//休息一会
        } catch (InterruptedException ire) {
        }
        //PoliceUpload policeUpload=new PoliceUpload();
      /*  policeUpload.upload();
        try {
            Thread.sleep(10000);//休息一会
        } catch (InterruptedException ire) {
        }*/
        carUpload.upload();
       /* CollectionLinkedQueue.offer(null);// 添加到本地对队列
        ExecutorService es = Executors.newFixedThreadPool(4);
        es.submit(new CollectionPoll());
        es.shutdown();*/

        //根据
        try {
            Thread.sleep(10000);//休息一会
        } catch (InterruptedException ire) {
        }
    }
}
